package cn.lzx.basic.service.impl;

import cn.lzx.basic.mapper.BaseMapper;
import cn.lzx.basic.query.BaseQuery;
import cn.lzx.basic.service.IBaseService;
import cn.lzx.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * service实现类基类
 * @Transactional事务注解 用于增删改
 * <T>泛型 谁继承泛型就是谁
 * @service写在具体的子类的类上 因为这个是基类不会通过service创建对象
 * @param <T>
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl <T> implements IBaseService <T> {
    
    @Autowired
    private BaseMapper<T> baseMapper;
    /**
     * 添加
     * @param t
     */
    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }
    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }
    /**
     * 修改
     * @param t
     */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }
    /**
     * 查询单个
     * @param id
     * @return
     */
    @Override
    public T loadById(Long id) {
        return  baseMapper.loadById(id);
    }
    /**
     * 查询全部
     * @return
     */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }
    /**
     * 分页查询
     * @param baseQuery
     * @return
     */
    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        Long totals = baseMapper.queryCount(baseQuery);
        if(totals == 0){
            return new PageList<>();
        }
        List<T> data = baseMapper.queryData(baseQuery);
        return new PageList<>(totals,data);
    }
    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }
}
